package com.agileboot.domain.bot.botQuestion;

import com.agileboot.common.core.page.PageDTO;
import com.agileboot.domain.bot.botQuestion.command.AddBotQuestionCommand;
import com.agileboot.domain.bot.botQuestion.command.UpdateBotQuestionCommand;
import com.agileboot.domain.bot.botQuestion.db.BotQuestionEntity;
import com.agileboot.domain.bot.botQuestion.db.BotQuestionService;
import com.agileboot.domain.bot.botQuestion.dto.BotQuestionDTO;
import com.agileboot.domain.bot.botQuestion.model.BotQuestionModel;
import com.agileboot.domain.bot.botQuestion.model.BotQuestionModelFactory;
import com.agileboot.domain.bot.botQuestion.query.BotQuestionQuery;
import com.agileboot.domain.common.command.BulkOperationCommand;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * BotQuestion 应用服务类
 */
@Service
@RequiredArgsConstructor
public class BotQuestionApplicationService {

    private final BotQuestionModelFactory modelFactory;
    private final BotQuestionService service;

    public PageDTO<BotQuestionDTO> getBotQuestionList(BotQuestionQuery query) {
        Page<BotQuestionEntity> page = service.page(query.toPage(), query.toQueryWrapper());
        List<BotQuestionDTO> records = page.getRecords().stream()
                .map(BotQuestionDTO::new)
                .collect(Collectors.toList());
        return new PageDTO<>(records, page.getTotal());
    }

    public List<BotQuestionDTO> getBotQuestionListAll(BotQuestionQuery query) {
        List<BotQuestionEntity> list = service.list(query.toQueryWrapper());
        return list.stream().map(BotQuestionDTO::new).collect(Collectors.toList());
    }

    public BotQuestionDTO getBotQuestionInfo(Integer id) {
        BotQuestionEntity byId = service.getById(id);
        return new BotQuestionDTO(byId);
    }

    public void addBotQuestion(AddBotQuestionCommand command) {
        BotQuestionModel model = modelFactory.create();
        model.loadFromAddCommand(command);
        model.checkQuestionUnique();
        model.insert();
    }

    public void updateBotQuestion(UpdateBotQuestionCommand command) {
        BotQuestionModel model = modelFactory.loadById(command.getId());
        model.loadFromUpdateCommand(command);
        model.checkQuestionUnique();
        model.updateById();
    }

    public void deleteBotQuestion(BulkOperationCommand<Integer> deleteCommand) {
        // 可按需添加逻辑校验
        service.removeBatchByIds(deleteCommand.getIds());
    }
}
